package com.ruoyi.project.stock.stock.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.bas.basJobId.service.IBasJobIdService;
import com.ruoyi.project.device.space.domain.BasCargoSpace;
import com.ruoyi.project.device.space.service.IBasCargoSpaceService;
import com.ruoyi.project.stock.stock.domain.BasStock;
import com.ruoyi.project.stock.stock.service.IBasStockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 库存表Controller
 * 
 * @author ruoyi
 * @date 2023-04-10
 */
@Controller
@RequestMapping("/stock/stock")
public class BasStockController extends BaseController
{
    private String prefix = "stock/stock";

    @Autowired
    private IBasStockService basStockService;
    @Autowired
    private IBasCargoSpaceService basCargoSpaceService;
    @Autowired
    private IBasJobIdService basJobIdService;

    @GetMapping()
    public String stock()
    {
        return prefix + "/stock";
    }

    @GetMapping("/pointOut")
    public String pointOut(){
        return "mydo/pointOut";
    }

    @GetMapping("/selectCargo")
    public String selectCargo(){
        return prefix+ "/selectCargo";
    }
    @GetMapping("/errorStock")
    public String errorStock(){
        return prefix+"/errorStock";
    }

    /**
     * 查询库存表列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasStock basStock)
    {
        startPage();
        List<BasStock> list = basStockService.selectBasStockList(basStock);
        return getDataTable(list);
    }

    /**
     * 导出库存表列表
     */
    @Log(title = "库存表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasStock basStock)
    {
        List<BasStock> list = basStockService.selectBasStockList(basStock);
        ExcelUtil<BasStock> util = new ExcelUtil<BasStock>(BasStock.class);
        return util.exportExcel(list, "库存表数据");
    }

    /**
     * 新增库存表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库存表
     */
    @Log(title = "库存表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HttpServletRequest request, BasStock basStock)
    {
        String stockId = basJobIdService.getJobId("KC");
        int sa = basStockService.insertBasStock(request,basStock, stockId);
        if (sa == 2){
            return AjaxResult.success("操作成功！");
        } else if (sa == 1){
            return AjaxResult.error("该托盘信息已经在其他货位存在，请检查！");
        } else {
            return AjaxResult.error("未知错误，请联系管理员！");
        }

    }

    /**
     * 修改库存表
     */
    @GetMapping("/edit/{stockId}")
    public String edit(@PathVariable("stockId") String stockId, ModelMap mmap)
    {
        BasStock basStock = basStockService.selectBasStockByStockId(stockId);
        mmap.put("basStock", basStock);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存表
     */
    @Log(title = "库存表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasStock basStock)
    {
        return toAjax(basStockService.updateBasStock(basStock));
    }

    /**
     * 删除库存表
     */
    @Log(title = "库存表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(basStockService.deleteBasStockByStockIds(ids));
    }

    @RequestMapping("/selectCargolist")
    @ResponseBody
    public TableDataInfo cargospaceList(String status,String warehouseId){
        BasCargoSpace basCargoSpace = new BasCargoSpace();
        basCargoSpace.setStatus(status);
        basCargoSpace.setWarehouseId(warehouseId);
        startPage();
        List<BasCargoSpace> basCargoSpaces = basCargoSpaceService.selectBasCargoSpaceList(basCargoSpace);
        return getDataTable(basCargoSpaces);
    }

    /**
    * 指定货位出库
    */
    @RequestMapping("/assignCargoOut")
    @ResponseBody
    public AjaxResult assignCargoOut(String trayCode,String exitId){
        String msg = basStockService.assignCargoOut(trayCode,exitId);
        return AjaxResult.success(msg);

    }

    /**
     * 查询异常库位库存表列表
     */
    @PostMapping("/errorStockList")
    @ResponseBody
    public TableDataInfo ErrorStockList(BasStock basStock)
    {
        startPage();
        List<BasStock> list = basStockService.selectErrorStockList(basStock);
        return getDataTable(list);
    }

}
